//
//  HomeViewController.swift
//  DouYuZB
//
//  Created by DSDL on 2016/10/25.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit
private let kTitleViewH: CGFloat = 40

class HomeViewController: UIViewController {
    //MARK: - 懒加载属性
    //titleView
    fileprivate lazy var pageTitleView:PageTitleView = { [weak self] in
        let titleFrame = CGRect(x: 0, y:64, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView =   PageTitleView(frame: titleFrame, title: titles)
        titleView.delegate = self
        return titleView
        }()
    //内容view
    fileprivate lazy var pageContentView: PAgeContentView = { [weak self] in
        //1.确定内容的frame
        let contentH = kScreenH-kStatusBarH - kNavigatiorBarH - kTitleViewH - kTabbarH
        let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavigatiorBarH + kTitleViewH, width: kScreenW, height: contentH)
        
        //2.添加所有的子控制器
        var childVcs = [UIViewController]()
        childVcs.append(RecommendViewController())
        childVcs.append(GameViewController())
        childVcs.append(AmuseViewController())
        childVcs.append(FunnyViewController())
        //3.创建 PAgeContentView
        let pageContentView = PAgeContentView(frame: contentFrame, childVcsArr: childVcs, parentViewController: self)
        pageContentView.delegate = self
        return pageContentView
        }()
    
    //MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置 UI 界面
        setupUI()
    }
}

//MARK: -  设置UI 界面
extension HomeViewController{
    
    func setupUI(){
        //0. 不要调整scrollerView的内变局
        automaticallyAdjustsScrollViewInsets = false
        //1.设置导航栏
        setupNavigationBar()
        
        //2.添加titleView
        view.addSubview(pageTitleView)
        //3.添加contentView
        view.addSubview(pageContentView)
        pageContentView.backgroundColor = UIColor.purple
    }
    
    func setupNavigationBar(){
        //0.初始化 btn
        let btn = UIButton()
        btn.setImage(UIImage(named: "logo"), for: UIControlState())
        btn.sizeToFit()
        
        //1.设置左侧的 Item
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo")
        
        //2.设置右侧的 Item
        let size = CGSize(width: 40, height: 40)
        let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size)
        let search = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_clicked", size: size)
        let qrcodeItem = UIBarButtonItem(imageName: "Image_scan", highImageName: "Image_scan_click", size: size)
        navigationItem.rightBarButtonItems = [historyItem, search, qrcodeItem]
    }
}
//MARK: -   PageTitleViewDelegate
extension HomeViewController: PageTitleViewDelegate {
    func pageTitleView(_ title: PageTitleView, selectedIndex index: Int) {
        pageContentView.setCurrentIndex(index)
    }
}
//MARK: -   PageContentViewDelegate
extension HomeViewController: PageContentViewDelegate {
    
    func pageContentView(_ content: PAgeContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitltWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
